package com.qj.yantaishan.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qj.yantaishan.common.core.domain.R;
import com.qj.yantaishan.common.utils.DateUtils;
import com.qj.yantaishan.common.utils.StringUtils;
import com.qj.yantaishan.domain.ProductGroup;
import com.qj.yantaishan.domain.ProductSku;
import com.qj.yantaishan.domain.ProductToGroup;
import com.qj.yantaishan.mapper.ShanglalaProductMapper;
import com.qj.yantaishan.service.*;
import com.qj.yantaishan.vo.ProductVo;
import com.qj.yantaishan.vo.ShanglalaProductVo;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.qj.yantaishan.common.annotation.Log;
import com.qj.yantaishan.common.enums.BusinessType;
import com.qj.yantaishan.domain.ShanglalaProduct;
import com.qj.yantaishan.common.core.controller.BaseController;
import com.qj.yantaishan.common.core.domain.AjaxResult;
import com.qj.yantaishan.common.utils.poi.ExcelUtil;
import com.qj.yantaishan.common.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author qihe
 * @date 2022-07-25
 */

@RestController
@RequestMapping("/yantaishan/ShanglalaProduct")
@SuppressWarnings("all")
public class ShanglalaProductController extends BaseController {
    private String prefix = "yantaishan/ShanglalaProduct";
    @Autowired
    private IProductVoService productVoService;
    @Autowired
    private ShanglalaProductMapper shanglalaProductMapper;
    @Autowired
    private IProductCategoryService productCategoryService;
    @Autowired
    private IProductSkuService productSkuService;
    @Autowired
    private IProductToGroupService productToGroupService;
    @Autowired
    private IProductGroupService productGroupService;
    @Autowired
    private IShanglalaProductService shanglalaProductService;
    @Autowired
    private IShanglalaProductVoService iShanglalaProductVoService;

    @RequiresPermissions("yantaishan:product:view")
    @GetMapping()
    public String product() {
        return prefix + "/product";
    }


    @GetMapping("test/product/{id}")
    public R getProduct(@PathVariable Long id) {
        ShanglalaProduct pro = shanglalaProductMapper.selectById(id);
        return R.ok(pro);
    }

    /**
     * 商品分页
     *
     * @param currentPage
     * @param pageSize
     * @param keyword
     * @param orderBy
     * @param sort
     * @return
     */
    @GetMapping("page")
    public R page(@RequestParam(required = false, defaultValue = "1") Integer currentPage, @RequestParam(required = false, defaultValue = "10") Integer pageSize, @RequestParam(required = false, defaultValue = "") String keyword, @RequestParam(required = false, defaultValue = "product_id") String orderBy, @RequestParam(required = false, defaultValue = "desc") String sort) {
        Page<ProductVo> page = new Page<>(currentPage, pageSize);
        QueryWrapper<ProductVo> queryWrapper = new QueryWrapper<ProductVo>().like("name", keyword);
        if (sort.equals("desc")) queryWrapper.orderByDesc(orderBy);
        else queryWrapper.orderByAsc(orderBy);

        Page<ProductVo> productVoPage = productVoService.getProductPage(page, sort, orderBy, keyword);
        Page<ProductVo> shanglalaProductPage = productVoService.page(page, queryWrapper);
        List<ProductVo> voList = shanglalaProductPage.getRecords().stream().map((productVo -> {
            productVo.setProductCategory(productCategoryService.getById(productVo.getStoreCategoryId()));

            List<ProductToGroup> productToGroups = productToGroupService.list(new LambdaQueryWrapper<ProductToGroup>().eq(ProductToGroup::getProductId, productVo.getProductId()));
            if (productToGroups != null && productToGroups.size() > 0) {
                String groupName = "";
                for (ProductToGroup productToGroup : productToGroups) {
                    ProductGroup group = productGroupService.getById(productToGroup.getGroupId());
                    if (StringUtils.isNotEmpty(groupName)) {
                        groupName += "," + group.getGroupName();
                    } else {
                        groupName += group.getGroupName();
                    }
                }
                productVo.setGroupName(groupName);

            } else {
                productVo.setGroupName("暂无分组");
            }


            return productVo;
        })).collect(Collectors.toList());
        shanglalaProductPage.setRecords(voList);
        return R.ok(shanglalaProductPage);
    }

    /**
     * 销售中商品
     *
     * @param currentPage
     * @param pageSize
     * @param keyword
     * @param orderBy
     * @param sort
     * @return
     */
    @GetMapping("sale/page")
    public R salePage(@RequestParam(required = false, defaultValue = "1") Integer currentPage, @RequestParam(required = false, defaultValue = "10") Integer pageSize, @RequestParam(required = false, defaultValue = "") String keyword, @RequestParam(required = false, defaultValue = "product_id") String orderBy, @RequestParam(required = false, defaultValue = "desc") String sort) {
        Page<ProductVo> page = new Page<>(currentPage, pageSize);
        QueryWrapper<ProductVo> queryWrapper = new QueryWrapper<ProductVo>().like("name", keyword);
        if (sort.equals("desc")) queryWrapper.orderByDesc(orderBy);
        else queryWrapper.orderByAsc(orderBy);
        queryWrapper.eq("soldout", 0).eq("status", 1);
        Page<ProductVo> shanglalaProductPage = productVoService.page(page, queryWrapper);
        List<ProductVo> voList = shanglalaProductPage.getRecords().stream().map((productVo -> {
            productVo.setProductCategory(productCategoryService.getById(productVo.getStoreCategoryId()));
            productVo.setSkuNum(productSkuService.selectCountByProductId(productVo.getProductId()));
            List<ProductToGroup> productToGroups = productToGroupService.list(new LambdaQueryWrapper<ProductToGroup>().eq(ProductToGroup::getProductId, productVo.getProductId()));
            if (productToGroups != null && productToGroups.size() > 0) {
                String groupName = "";
                for (ProductToGroup productToGroup : productToGroups) {
                    ProductGroup group = productGroupService.getById(productToGroup.getGroupId());
                    if (StringUtils.isNotEmpty(groupName)) {
                        groupName += "," + group.getGroupName();
                    } else {
                        groupName += group.getGroupName();
                    }
                }
                productVo.setGroupName(groupName);

            } else {
                productVo.setGroupName("暂无分组");
            }

            return productVo;
        })).collect(Collectors.toList());
        shanglalaProductPage.setRecords(voList);
        return R.ok(shanglalaProductPage);

    }

    /**
     * 已售罄商品
     *
     * @param currentPage
     * @param pageSize
     * @param keyword
     * @param orderBy
     * @param sort
     * @return
     */
    @GetMapping("saleEnd/page")
    public R saleEndPage(@RequestParam(required = false, defaultValue = "1") Integer currentPage, @RequestParam(required = false, defaultValue = "10") Integer pageSize, @RequestParam(required = false, defaultValue = "") String keyword, @RequestParam(required = false, defaultValue = "product_id") String orderBy, @RequestParam(required = false, defaultValue = "desc") String sort) {
        Page<ProductVo> page = new Page<>(currentPage, pageSize);
        QueryWrapper<ProductVo> queryWrapper = new QueryWrapper<ProductVo>().like("name", keyword);
        if (sort.equals("desc")) queryWrapper.orderByDesc(orderBy);
        else queryWrapper.orderByAsc(orderBy);
        queryWrapper.eq("quantity", 0);

        queryWrapper.eq("status", 1);
        Page<ProductVo> shanglalaProductPage = productVoService.page(page, queryWrapper);
        List<ProductVo> voList = shanglalaProductPage.getRecords().stream().map((productVo -> {
            productVo.setSkuNum(productSkuService.selectCountByProductId(productVo.getProductId()));
            productVo.setProductCategory(productCategoryService.getById(productVo.getStoreCategoryId()));
            List<ProductToGroup> productToGroups = productToGroupService.list(new LambdaQueryWrapper<ProductToGroup>().eq(ProductToGroup::getProductId, productVo.getProductId()));
            if (productToGroups != null && productToGroups.size() > 0) {
                String groupName = "";
                for (ProductToGroup productToGroup : productToGroups) {
                    ProductGroup group = productGroupService.getById(productToGroup.getGroupId());
                    if (StringUtils.isNotEmpty(groupName)) {
                        groupName += "," + group.getGroupName();
                    } else {
                        groupName += group.getGroupName();
                    }
                }
                productVo.setGroupName(groupName);

            } else {
                productVo.setGroupName("暂无分组");
            }
            return productVo;
        })).collect(Collectors.toList());
        shanglalaProductPage.setRecords(voList);
        return R.ok(shanglalaProductPage);
    }

    /**
     * 仓库中商品
     *
     * @param currentPage
     * @param pageSize
     * @param keyword
     * @param orderBy
     * @param sort
     * @return
     */
    @GetMapping("store/page")
    public R storePage(@RequestParam(required = false, defaultValue = "1") Integer currentPage, @RequestParam(required = false, defaultValue = "10") Integer pageSize, @RequestParam(required = false, defaultValue = "") String keyword, @RequestParam(required = false, defaultValue = "product_id") String orderBy, @RequestParam(required = false, defaultValue = "desc") String sort) {
        Page<ProductVo> page = new Page<>(currentPage, pageSize);
        QueryWrapper<ProductVo> queryWrapper = new QueryWrapper<ProductVo>().like("name", keyword);
        if (sort.equals("desc")) queryWrapper.orderByDesc(orderBy);
        else queryWrapper.orderByAsc(orderBy);



        queryWrapper.eq("status", 0);

        Page<ProductVo> shanglalaProductPage = productVoService.page(page, queryWrapper);
        List<ProductVo> voList = shanglalaProductPage.getRecords().stream().map((productVo -> {
            productVo.setSkuNum(productSkuService.selectCountByProductId(productVo.getProductId()));
            productVo.setProductCategory(productCategoryService.getById(productVo.getStoreCategoryId()));
            List<ProductToGroup> productToGroups = productToGroupService.list(new LambdaQueryWrapper<ProductToGroup>().eq(ProductToGroup::getProductId, productVo.getProductId()));
            if (productToGroups != null && productToGroups.size() > 0) {
                String groupName = "";
                for (ProductToGroup productToGroup : productToGroups) {
                    ProductGroup group = productGroupService.getById(productToGroup.getGroupId());
                    if (StringUtils.isNotEmpty(groupName)) {
                        groupName += "," + group.getGroupName();
                    } else {
                        groupName += group.getGroupName();
                    }
                }
                productVo.setGroupName(groupName);

            } else {
                productVo.setGroupName("暂无分组");
            }
            return productVo;
        })).collect(Collectors.toList());
        shanglalaProductPage.setRecords(voList);
        return R.ok(shanglalaProductPage);
    }

    @PostMapping("save")
    public R saveProduct(@RequestBody String productVo) {

//        System.out.println(productVo);
        ProductVo productVo1 = JSON.parseObject(productVo, ProductVo.class);
        productVo1.setStoreId(1);
        Date date = new Date();
        productVo1.setDateAdded(String.valueOf(date.getTime() / 1000));

        int flag = productVoService.mySave(productVo1);
        return R.ok();
    }


    @PutMapping("update")
    public R update(@RequestBody ProductVo productVo) {
        int flag = productVoService.myUpdate(productVo);
        return R.ok();
    }

    @GetMapping("details")
    public R getProductDetails(@RequestParam(required = true) Integer productId) {
        ProductVo productVo = productVoService.getProductDetails(productId);
        return R.ok(productVo);
    }

    @DeleteMapping("delete")
    public R delete(@RequestParam Integer productId) {
        int flag = productVoService.delete(productId);
        return R.ok();
    }
    /**
     * 获取首页显示商品
     *
     * @param group_id 商品分类
     * */
    @RequestMapping("/showProduct")
    //Integer group_id商品组id
    public R show(Integer group_id){
        QueryWrapper<ShanglalaProductVo> wrapper=new QueryWrapper<>();
        wrapper.eq("status",1);
        wrapper.eq("group_id",group_id);
        wrapper.eq("platform_recommend",1);
        wrapper.orderByDesc("date_added");
        wrapper.last("limit 0,6");
        List<ShanglalaProductVo> list=iShanglalaProductVoService.list(wrapper);
        return R.ok(list);
    }
    @RequestMapping("/moreProduct")
    /**
     * 获取更多分类商品
     *
     * @param group_id 商品分类
     * @param Current 当前页码
     * */
    public R GetMoreProductAcess(Integer group_id, Integer Current){
        Page<ShanglalaProductVo> page = new Page<>(Current,10);
        QueryWrapper<ShanglalaProductVo> wrapper=new QueryWrapper<>();
        wrapper.eq("status",1);
        wrapper.eq("group_id",group_id);
        wrapper.orderByDesc("date_added");
        Page<ShanglalaProductVo> page1=iShanglalaProductVoService.page(page,wrapper);
        List<ShanglalaProductVo> records = productVoService.handleSkuList(page1.getRecords());
        page1.setRecords(records);
        return R.ok(page1);
    }
}
